package com.xdh.zzqh.ui.fragment

import android.content.Intent
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ArrayAdapter
import android.widget.AutoCompleteTextView
import android.widget.Button
import android.widget.TextView
import androidx.fragment.app.Fragment
import com.xdh.zzqh.R
import com.xdh.zzqh.store.Product
import com.xdh.zzqh.store.User
import com.xdh.zzqh.ui.OrderActivity
import com.xdh.zzqh.ui.view.LabelInput
import com.xdh.zzqh.ui.view.LabelTextInput
import com.xdh.zzqh.ui.view.Order
import com.xdh.zzqh.ui.view.showViews
import com.xdh.zzqh.utils.currentDate
import com.xdh.zzqh.utils.globalOrderInfo
import com.xdh.zzqh.utils.onClick
import com.xdh.zzqh.utils.remarkSplitLine
import com.xdh.zzqh.utils.runOnUiWithInit
import com.xdh.zzqh.utils.showToast
import com.xdh.zzqh.utils.stringById
import java.math.BigDecimal

class OrderFragment : Fragment() {

    private lateinit var mCTName: AutoCompleteTextView
    private lateinit var mLtiDate: LabelTextInput
    private lateinit var mTvOrderNum: TextView
    private lateinit var mBtnReset: Button
    private lateinit var mBtnSelect: Button
    private lateinit var mBtnAdd: Button

    private lateinit var mLtiType: LabelTextInput
    private lateinit var mLtiStyle: LabelTextInput
    private lateinit var mLtiSpec: LabelTextInput
    private lateinit var mLtiDir: LabelTextInput
    private lateinit var mLtiLeft: LabelInput
    private lateinit var mLtiRight: LabelInput
    private lateinit var mLtiPrice: LabelTextInput
    private lateinit var mLtiHands: LabelTextInput
    private lateinit var mLtiColor: LabelTextInput
    private lateinit var mLtiRemark: LabelTextInput
    private lateinit var mTCProduct: AutoCompleteTextView

    private lateinit var mLtiDis: LabelTextInput
    private lateinit var mLtiDis2: LabelTextInput
    private lateinit var mBtnCalc: Button

    private lateinit var mBtnCreate: Button

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        val view = inflater.inflate(R.layout.fragment_order, null)
        initUi(view)
        return view
    }

    private fun initUi(view: View) {
        mCTName = view.findViewById(R.id.id_order_user_name)
        mLtiDate = view.findViewById(R.id.id_order_date)
        mTvOrderNum = view.findViewById(R.id.id_order_tv_num)
        mBtnReset = view.findViewById(R.id.id_order_btn_reset)
        mBtnSelect = view.findViewById(R.id.id_order_product_btn_select)
        mBtnAdd = view.findViewById(R.id.id_order_btn_add)
        mTCProduct = view.findViewById(R.id.id_order_tv_product)

        mLtiType = view.findViewById(R.id.id_order_lti_type)
        mLtiStyle = view.findViewById(R.id.id_order_lti_style)
        mLtiSpec = view.findViewById(R.id.id_order_lti_spec)
        mLtiDir = view.findViewById(R.id.id_order_lti_dir)
        mLtiLeft = view.findViewById(R.id.id_order_lti_left)
        mLtiRight = view.findViewById(R.id.id_order_lti_right)
        mLtiPrice = view.findViewById(R.id.id_order_lti_price)
        mLtiHands = view.findViewById(R.id.id_order_lti_hands)
        mLtiColor = view.findViewById(R.id.id_order_lti_color)
        mLtiRemark = view.findViewById(R.id.id_order_lti_remark)

        mBtnCreate = view.findViewById(R.id.id_order_btn_create)

        mLtiDis = view.findViewById(R.id.id_order_lti_dis)
        mLtiDis2 = view.findViewById(R.id.id_order_lti_dis2)

        mLtiDate.setText( currentDate() )
        mTvOrderNum.text = "0"

        mCTName.setOnFocusChangeListener { _, hasFocus ->
            if (hasFocus) {
                mCTName.showDropDown()
            }
        }
        mCTName.setOnClickListener {
            mCTName.showDropDown()
        }
        mTCProduct.setOnFocusChangeListener { _, hasFocus ->
            if (hasFocus) {
                mTCProduct.showDropDown()
            }
        }
        mTCProduct.setOnClickListener { mTCProduct.showDropDown() }

        mTCProduct.setOnItemClickListener { parent, _, position, _ ->
            runOnUiWithInit {

                try {
                    val name = parent.getItemAtPosition(position).toString()
                    val product = Product.queryWithName(name)
                    mLtiType.setText(product.ty)
                    mLtiStyle.setText(product.style)
                    mLtiSpec.setText(product.spec)
                    mLtiDir.setText(product.dir)
                    mLtiPrice.setText(product.price)
                    mLtiHands.setText(product.hands)
                    mLtiColor.setText(product.color)
                } catch (e: Exception) {
                    e.printStackTrace()
                }

            }
        }

        mBtnSelect.onClick {

            val name = mTCProduct.text.toString()
            if (name.isNotEmpty()) {
                try {
                    val product = Product.queryWithName(name)
                    mLtiType.setText(product.ty)
                    mLtiStyle.setText(product.style)
                    mLtiSpec.setText(product.spec)
                    mLtiDir.setText(product.dir)
                    mLtiPrice.setText(product.price)
                    mLtiHands.setText(product.hands)
                    mLtiColor.setText(product.color)
                } catch (e: Exception) {
                    e.printStackTrace()
                }
            }

            val ps = Product.queryAll()
            val list = ps.map { it.name }

            showViews(mBtnAdd, list) { index, _ ->
                val product = ps[index]
                mLtiType.setText(product.ty)
                mLtiStyle.setText(product.style)
                mLtiSpec.setText(product.spec)
                mLtiDir.setText(product.dir)
                mLtiPrice.setText(product.price)
                mLtiHands.setText(product.hands)
                mLtiColor.setText(product.color)
            }
        }

        mBtnReset.setOnClickListener {
            globalOrderInfo.order.clear()
            updateNum()
        }

        mBtnAdd.setOnClickListener {
            try {
                val ty = mLtiType.text()
                val style = mLtiStyle.text()
                val spec = mLtiSpec.text()
                val dir = mLtiDir.text()
                val left = mLtiLeft.textInt()
                val right = mLtiRight.textInt()
                val price = mLtiPrice.text()
                val hands = mLtiHands.text()
                val color = mLtiColor.text()
                val remark = mLtiRemark.text()
                val dis = mLtiDis.text().trim()
                val dis2 = mLtiDis2.text().trim()

                val order = Order(
                    ty, style, spec, dir, left, right, BigDecimal(price), hands, color, remarkSplitLine(remark), dis, dis2
                )
                globalOrderInfo.order.add(order)
                updateNum()

            } catch (e: Exception) {
                e.printStackTrace()
                showToast(e.message ?: stringById(R.string.cuo_wu))
            }
        }

        mBtnCreate.setOnClickListener {
            globalOrderInfo.name = mCTName.text.toString()
            globalOrderInfo.date = mLtiDate.text()
            val intent = Intent(requireContext(), OrderActivity::class.java)
            requireContext().startActivity(intent)
        }

    }

    private fun updateNum() {
        mTvOrderNum.text = "${globalOrderInfo.order.size}"
    }

    override fun onResume() {
        super.onResume()
        runOnUiWithInit {
            val users = User.queryAll()
            mCTName.setAdapter(ArrayAdapter(requireContext(), android.R.layout.simple_dropdown_item_1line, users))
            mCTName.threshold = 1

            val ps = Product.queryAll()
            val ns = ps.map { it.name }
            mTCProduct.setAdapter(ArrayAdapter(requireContext(), android.R.layout.simple_dropdown_item_1line, ns))
            mTCProduct.threshold = 1
        }
    }
}















